package com.pai4j.message.facade;

import com.pai4j.common.enums.InteractionMessageTypeEnum;
import com.pai4j.common.enums.community.ResourceTypeEnum;
import com.pai4j.domain.vo.dto.BaseMessageDTO;
import com.pai4j.message.builder.interaction.*;
import com.pai4j.message.builder.system.SystemMessageBuilder;
import com.pai4j.message.core.builder.MessageBuilder;
import com.pai4j.message.factory.MessageBuilderFactory;
import com.pai4j.message.model.SendResult;
import lombok.extern.slf4j.Slf4j;

/**
 * 消息系统外观类
 * 提供统一、简洁的消息构建和发送API
 *
 * 这是整个消息SDK的核心入口，隐藏了内部复杂性，提供流式API
 *
 * @author PAI4J Team
 * @date 2025-11-01
 */
@Slf4j
public class MessageFacade {

    /**
     * 互动消息入口
     *
     * @return 互动消息构建器
     */
    public static InteractionMessageFacade interaction() {
        return new InteractionMessageFacade();
    }

    /**
     * 系统消息入口
     *
     * @return 系统消息构建器
     */
    public static SystemMessageFacade system() {
        return new SystemMessageFacade();
    }

    /**
     * 通用消息构建器（基于类型）
     *
     * @param type 消息类型
     * @return 消息构建器
     */
    public static <T extends BaseMessageDTO> MessageBuilder<T> builder(InteractionMessageTypeEnum type) {
        MessageBuilder<T> builder = MessageBuilderFactory.getBuilder(type);
        if (builder == null) {
            log.warn("未找到对应的MessageBuilder: type={}", type);
        }
        return builder;
    }

    /**
     * 互动消息外观
     */
    public static class InteractionMessageFacade {

        /**
         * 点赞消息
         */
        public LikeMessageBuilder like() {
            return LikeMessageBuilder.create();
        }

        /**
         * 评论消息
         */
        public CommentMessageBuilder comment() {
            return CommentMessageBuilder.create();
        }

        /**
         * 回复消息
         */
        public ReplyMessageBuilder reply() {
            return ReplyMessageBuilder.create();
        }

        /**
         * 收藏消息
         */
        public CollectMessageBuilder collect() {
            return CollectMessageBuilder.create();
        }

        /**
         * 关注消息
         */
        public FollowMessageBuilder follow() {
            return FollowMessageBuilder.create();
        }

        /**
         * 快速发送点赞消息（最简化API）
         *
         * @param senderId 发送者ID
         * @param resourceType 资源类型
         * @param resourceId 资源ID
         * @return 发送结果
         */
        public SendResult sendLike(String senderId, ResourceTypeEnum resourceType, String resourceId) {
            return like()
                .sender(senderId)
                .resource(resourceType, resourceId)
                .send();
        }

        /**
         * 快速发送评论消息
         *
         * @param senderId 发送者ID
         * @param resourceType 资源类型
         * @param resourceId 资源ID
         * @param content 评论内容
         * @return 发送结果
         */
        public SendResult sendComment(String senderId, ResourceTypeEnum resourceType, String resourceId, String content) {
            return comment()
                .sender(senderId)
                .resource(resourceType, resourceId)
                .content(content)
                .send();
        }

        /**
         * 快速发送收藏消息
         *
         * @param senderId 发送者ID
         * @param resourceType 资源类型
         * @param resourceId 资源ID
         * @return 发送结果
         */
        public SendResult sendCollect(String senderId, ResourceTypeEnum resourceType, String resourceId) {
            return collect()
                .sender(senderId)
                .resource(resourceType, resourceId)
                .send();
        }

        /**
         * 快速发送关注消息
         *
         * @param senderId 发送者ID
         * @param receiverId 接收者ID
         * @return 发送结果
         */
        public SendResult sendFollow(String senderId, String receiverId) {
            return follow()
                .sender(senderId)
                .receiver(receiverId)
                .send();
        }
    }

    /**
     * 系统消息外观
     */
    public static class SystemMessageFacade {

        /**
         * 通知消息
         */
        public SystemMessageBuilder notification() {
            return SystemMessageBuilder.create();
        }

        /**
         * 快速发送系统通知
         *
         * @param receiverId 接收者ID
         * @param content 消息内容
         * @return 发送结果
         */
        public SendResult sendNotification(String receiverId, String content) {
            return notification()
                .receiver(receiverId)
                .content(content)
                .send();
        }

        /**
         * 快速发送活动消息
         *
         * @param receiverId 接收者ID
         * @param content 消息内容
         * @param activityName 活动名称
         * @param activityUrl 活动链接
         * @return 发送结果
         */
        public SendResult sendActivity(String receiverId, String content, String activityName, String activityUrl) {
            return notification()
                .receiver(receiverId)
                .content(content)
                .activity(activityName, activityUrl)
                .send();
        }

        /**
         * 批量发送系统通知
         *
         * @param receiverIds 接收者ID列表
         * @param content 消息内容
         * @return 发送结果列表
         */
        public java.util.List<SendResult> batchSendNotification(String[] receiverIds, String content) {
            java.util.List<SendResult> results = new java.util.ArrayList<>();
            if (receiverIds == null || receiverIds.length == 0) {
                return results;
            }

            for (String receiverId : receiverIds) {
                results.add(sendNotification(receiverId, content));
            }
            return results;
        }
    }

    /**
     * 私有构造函数，防止实例化
     */
    private MessageFacade() {
    }
}

